package com.example.gaming;

import com.example.ajouconqueror.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class conqueringActingMission4 extends Activity implements
		SensorEventListener {

	private TextView textMove;
	private int num = 0;
	SensorManager sensorManager = null;
	final Intent intent = getIntent();
	private Sensor aSensor;
	private final double calibration = SensorManager.STANDARD_GRAVITY;
	private static final int REQUEST_QUESTION_MISSION = 101;
	MediaPlayer backgroundPlayer;
	SoundPool pool;
	int track1, track2;
	boolean[] loaded = new boolean[3];
	private Vibrator vibrator;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		setContentView(R.layout.activity_conquering_acting_mission4);
		// just some textviews, for data output
		textMove = (TextView) findViewById(R.id.textturn);
		aSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

		backgroundPlayer = MediaPlayer.create(this, R.raw.test_cbr);
		backgroundPlayer.setLooping(true);

		pool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
		pool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
			@Override
			public void onLoadComplete(SoundPool soundPool, int sampleId,
					int status) {
				Log.d("sound", "Loaded: " + sampleId);
				if (track1 == sampleId)
					loaded[0] = true;
				else if (track2 == sampleId)
					loaded[1] = true;
			}
		});

		track1 = pool.load(this, R.raw.track1, 0);
		track2 = pool.load(this, R.raw.back, 0);

		String vibratorService = Context.VIBRATOR_SERVICE;
		vibrator = (Vibrator) getSystemService(vibratorService);

	}

	@Override
	protected void onPause() {
		pool.autoPause();
		backgroundPlayer.pause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		pool.autoResume();
		backgroundPlayer.start();
		sensorManager.registerListener(this, aSensor,
				sensorManager.SENSOR_DELAY_GAME);
	}

	@Override
	protected void onStop() {
		super.onStop();
		sensorManager.unregisterListener(this);
	}

	protected void onDestroy() {
		// release sound pool resources
		pool.release();
		if (backgroundPlayer.isPlaying())
			backgroundPlayer.stop();
		backgroundPlayer.release();
		super.onDestroy();
	}

	public void onBackPressed() {
		doVibration2();

		if (loaded[1]) {
			pool.play(track2, 1, 1, 0, 0, 1);
		}
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		synchronized (this) {
			if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

				float x = event.values[0];
				float y = event.values[1];
				float z = event.values[2];

				double a = Math.round(Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)
						+ Math.pow(z, 2)));
				float sensor = (float) (Math.abs((float) (a - calibration)) / calibration);

				// textMove.setText(sensor + "");
				if (sensor > 2) {

					num += 1;
					if (loaded[0]) {
						pool.play(track1, 1, 1, 0, 0, 0.5f);
					}
					doVibration();

					textMove.setText(Integer.toString(num));

				}

				if (num == 3) {
					Intent qIntent = new Intent(getApplicationContext(), conqueringQuestionMission.class);
//					qIntent.putExtra("building", intent.getStringExtra("buidling"));
					startActivityForResult(qIntent, REQUEST_QUESTION_MISSION);
					setResult(RESULT_OK, intent);
					finish();
				}

			}
		}

	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			if (requestCode == REQUEST_QUESTION_MISSION) {
				Toast.makeText(this, "you got point.", Toast.LENGTH_SHORT).show();
				setResult(RESULT_OK, intent);
				finish();
			}
		}
	}

	private void doVibration() {

		// pattern: off, on, off, on,....
		long[] pattern = { 0, 1000 };

		// vibrate using the pattern. Second argument sets repetition from
		// index 0. Use -1 if you want repetition.
		vibrator.vibrate(pattern, -1);

		// vibrator.vibrate(1000); // vibrate for 1 second (without pattern)

	}

	private void doVibration2() {

		// pattern: off, on, off, on,....
		long[] pattern = { 0, 4000 };

		// vibrate using the pattern. Second argument sets repetition from
		// index 0. Use -1 if you
		vibrator.vibrate(pattern, -1);

		// vibrator.vibrate(1000); // vibrate for 1 second (without pattern)

	}

}
